package com.example.demo.responseBodyAdvice.demo5.controller;

import com.example.demo.excel.service.ImportExcelBaseService;
import com.example.demo.responseBodyAdvice.demo5.util.EncryptAspect;
import com.example.demo.responseBodyAdvice.demo5.db.entity.Student;
import com.example.demo.responseBodyAdvice.demo5.db.repository.StudentRepository;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;

/**
 * @Author: zjq
 * @Date: create in 2023/3/28 14:46
 */
@Service
public class StudentServiceImpl extends ImportExcelBaseService implements StudentService {


    @Autowired
    private StudentRepository studentRepository;

    @Override
    public Map<String, List<Student>> importExcel(MultipartFile file) {
        Map<String, List<Student>> map = new HashMap<String, List<Student>>();

        int rowNum = 0;// 已取值的行数s
        int colNum = 0;// 列号
        int realRowCount = 0;// 真正有数据的行数
        // 得到工作空间
        Workbook workbook = null;

        try {
            workbook = super.getWorkbookByInputStream(file.getInputStream(), file.getOriginalFilename());
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 得到工作表
        Sheet sheet = super.getSheetByWorkbook(workbook, 0);
        if (sheet.getRow(20000) != null) {
            throw new RuntimeException("单批次导入必须小于或等于20000行！");
        }
        realRowCount = sheet.getPhysicalNumberOfRows();//读取所有行

        List<Student> liste = new ArrayList<>();
        List<Student> listadd = new ArrayList<Student>();

        Date date = new Date();

        for (Row row : sheet) {
            if (realRowCount == rowNum) {
                break;
            }
            if (super.isBlankRow(row)) {// 空行跳过
                continue;
            }
            if (row.getRowNum() == -1) {
                continue;
            } else {
                if (row.getRowNum() == 0 || row.getRowNum() == 1) {// 表头跳过
                    continue;
                }
            }
            rowNum++;
            colNum = 0;

            Student student = new Student();
            String errorMsg = "";

            // 姓名
            String name = super.getCellValue(sheet, row, colNum);
            if ("".equals(name)) {
                if (!"".equals(errorMsg)) {
                    errorMsg += ",";
                }
                errorMsg += "学生姓名不能为空";
            } else if (name.length() > 32) {
                if (!"".equals(errorMsg)) {
                    errorMsg += ",";
                }
                errorMsg += "学生姓名过长";
            } else {
                student.setName(name);
            }

            // 设置性别
            String sex = super.getCellValue(sheet, row, ++colNum);
            if (StringUtils.isNotEmpty(sex)) {
                if ("男".equals(sex)) {
                    student.setSex("1");
                } else if ("女".equals(sex)) {
                    student.setSex("0");
                } else {
                    student.setSex("2");
                }
            }

            // 设置身份证
            String sfz = super.getCellValue(sheet, row, ++colNum);
            if (StringUtils.isEmpty(sfz)) {
                if (!"".equals(errorMsg)) {
                    errorMsg += ",";
                }
                errorMsg += "身份证不能为空";
            } else if (sfz.length() > 18) {
                if (!"".equals(errorMsg)) {
                    errorMsg += ",";
                }
                errorMsg += "身份证过长";
            } else {
                student.setIdcard(sfz);
            }

            // 设置身份证
            String sfz2 = super.getCellValue(sheet, row, ++colNum);
            if (StringUtils.isEmpty(sfz2)) {
                if (!"".equals(errorMsg)) {
                    errorMsg += ",";
                }
                errorMsg += "身份证不能为空";
            } else if (sfz2.length() > 18) {
                if (!"".equals(errorMsg)) {
                    errorMsg += ",";
                }
                errorMsg += "身份证过长";
            } else {
                student.setSfzjh(sfz2);
            }

            String xh = super.getCellValue(sheet, row, ++colNum);
            student.setXh(xh);

            String age = super.getCellValue(sheet, row, ++colNum);
            student.setAge(age);

            // 没有错误信息可以保存
            if ("".equals(errorMsg)) {
                listadd.add(student);
            } else {
                // 错误信息需要记录
                student.setName(name);
                student.setSex(sex);
                student.setAge(age);
                student.setErrorMsg(errorMsg);
                liste.add(student);
            }

        }
        EncryptAspect.encrypt(listadd);
        studentRepository.saveAll(listadd);
        map.put("liste", liste);
        return map;
    }
}
